package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.AuthorBook;

public class AuthorBookDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(AuthorBook author_book) {
		open();
		this.sql = "INSERT INTO Author_Books (id_book, id_author) VALUES (?, ?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, author_book.getIdBook());
			this.statement.setInt(2, author_book.getIdAuthor());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}

	}

	public void update(AuthorBook author_book) {
		open();
		this.sql = "UPDATE Authors_Books SET id_book = ? WHERE id_author = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, author_book.getIdBook());
			this.statement.setInt(2, author_book.getIdAuthor());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(AuthorBook author_book) {
		open();
		this.sql = "DELETE FROM Authors_Books WHERE id_author = ? AND id_book = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, author_book.getIdAuthor());
			this.statement.setInt(2, author_book.getIdBook());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public AuthorBook search(Integer id_author) {
		open();
		this.sql = "SELECT * FROM Authors_Books WHERE id_author = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_author);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new AuthorBook(result.getInt("id_author"),
						result.getInt("id_book"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<AuthorBook> list() {
		open();
		this.sql = "SELECT * FROM Authors_Books";
		ArrayList<AuthorBook> authors_books = new ArrayList<AuthorBook>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				authors_books.add(new AuthorBook(result.getInt("id_author"),
						result.getInt("id_book")));
			}
			return authors_books;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
